<script type="text/javascript" src="paging.js"></script>
<form action="index.php?act=hocvien_xeplop_sub" method="POST" >
<div class="Left">
	
<?php
	include_once "../classes/hocvien_bs.php";
	
	$Key = isset($_GET['key']) ? trim($_GET['key']) : "";
	$MaKhoa = isset($_GET['mkh']) ? trim($_GET['mkh']) : "";
	$MaThoiGianHoc = isset($_GET['mtgh']) ? trim($_GET['mtgh']) : "";
	$MaChuongTrinh = isset($_GET['mct']) ? trim($_GET['mct']) : "";
	$Page    = isset($_GET['pag']) ? $_GET['pag'] : 1;
	
	$HocVien = new hocvien_bs();
	$HocVien->SetDaXoa(0);
	$HocVien->SetMaLopHoc('_'); //SET NULL , sẽ sử lý trong hocvien_db
	
	if($MaThoiGianHoc != "")
		$HocVien->SetMaThoiGianHoc($MaThoiGianHoc);
	if($MaChuongTrinh != "")
		$HocVien->SetMaChuongTrinh($MaChuongTrinh);
	if($Key != "")
		$HocVien->SetHoTen($Key);
	if($MaKhoa != "")
	{
		$DHocVien = $HocVien->DSHVTheoKhoa($MaKhoa);
		$TongSoHocVien = $DHocVien == null ? 0 : count($DHocVien);
		$Limit = 20;
		$SoTrang = ceil(count($DHocVien) / $Limit);
		
		$DHocVien = $HocVien->DSHVTheoKhoaPaging($MaKhoa, ($Page - 1) * $Limit, $Limit);
	}
	else
	{
		$DHocVien = $HocVien->LayDanhSach();
		$TongSoHocVien = $DHocVien == null ? 0 : count($DHocVien);
		$Limit = 20;
		$SoTrang = ceil(count($DHocVien) / $Limit);
		
		$DHocVien = $HocVien->LayDanhSachPaging(($Page - 1) * $Limit, $Limit);
	}
?>
<div class="Paging">
	<?php
		if($DHocVien != null)
		{
			if($Page > 1)
				echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",1);'>Đầu</a>";	
			for($i = 1; $i <= $SoTrang; $i++)
			{
				if($Page - 5 <= $i && $i <= $Page + 5)
				{
					if($i == $Page)
						echo "<a class='AjaxPageCur' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",$i);'>$i</a>";
					else
						echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",$i);'>$i</a>";
				}
				elseif($i == $Page + 6)
					echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",$i);'>...</a>";
				elseif($i == $Page - 6)
					echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",$i);'>...</a>";
			}
			if($Page < $SoTrang)
				echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",$SoTrang);'>Cuối</a>";	
			
			echo "<span style='float:right; background-color:#4ABDF4; color: #FFF; padding:0px 3px'>Trang $Page/$SoTrang</span>";
		}
	?>
</div>
<div id="KetQuaTimKiem">
	<table id="DataReport">
		<tr>
			<th>
				<input type="checkbox" onclick="javascript:checkAll(this)" />
			</th>
			<th  class="first">STT</th>
			<th  width="70px">Mã học viên</th>
			<th  colspan="2">Họ tên</th>
			<th >Ngày sinh<br />(d/M/y)</th>
			<th >Điện thoại</th>
		</tr>
		
		<!-- Lấy danh sách học viên từ cơ sở dữ liệu -->
		<?php
			if($DHocVien == null)
			{
				echo '<tr><td colspan="11" align="center"> Nội dung rỗng</td></tr>';
			}
			else
			{
				for($i = 0; $i < count($DHocVien); $i++)
				{
					$HT = TachHoTen($DHocVien[$i]['HoTen']);
					$LinkDetails = 'index.php?act=hocvien_chitiet&id=' . $DHocVien[$i]['MaHocVien'] . '&pin=' . $DHocVien[$i]['PIN'];
					$LinkUpdate = 'index.php?act=hocvien_capnhat&id=' . $DHocVien[$i]['MaHocVien'] . '&pin=' . $DHocVien[$i]['PIN'];;
					echo '<tr onclick="checkCheckbox(\'' . $DHocVien[$i]['MaHocVien'] . '\');" class="ButtonDetails" >';
						
						echo '<td align="center"><input type="checkbox" id="' . $DHocVien[$i]['MaHocVien'] . '" name="invite[]" value="' . $DHocVien[$i]['MaHocVien'] . '" /></td>';
						echo '<td align="center"><b>' . (($Page - 1) * $Limit + ($i + 1)) . '</b></td>';
						echo '<td>' . $DHocVien[$i]['MaHocVien'] . '</td>';
						echo '<td>' . $HT[0] . '</td>';
						echo '<td>' . $HT[1] . '</td>';
						echo '<td align="center">' . ConvertDate($DHocVien[$i]['NgaySinh']) . '</td>';
						echo '<td align="right">' . DinhDangSDT($DHocVien[$i]['DienThoai']) . '</td>';
					echo '</tr>';
					
				}
			}
		?>
	</table>
</div>
<div class="Paging">
	<?php
		if($DHocVien != null)
		{
			if($Page > 1)
				echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",1);'>Đầu</a>";	
			for($i = 1; $i <= $SoTrang; $i++)
			{
				if($Page - 5 <= $i && $i <= $Page + 5)
				{
					if($i == $Page)
						echo "<a class='AjaxPageCur' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",$i);'>$i</a>";
					else
						echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",$i);'>$i</a>";
				}
				elseif($i == $Page + 6)
					echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",$i);'>...</a>";
				elseif($i == $Page - 6)
					echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",$i);'>...</a>";
			}
			if($Page < $SoTrang)
				echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLXepLop(\"$Key\",\"$MaThoiGianHoc\",\"$MaChuongTrinh\",\"$MaKhoa\",$SoTrang);'>Cuối</a>";	
			echo "<span style='float:right; background-color:#4ABDF4; color: #FFF; padding:0px 3px'>Trang $Page/$SoTrang</span>";
		}
	?>
</div>

</div>

<div class="Right">		
	<table width="100%" style="border:1px solid #f1f1f1">
		<tr>
			<th style="background-color: #f1f1f1;">Số lượng </th>
		<tr>
		</tr>
			<td align="center">
				<?php echo $TongSoHocVien; ?> học viên
			</td>
		</tr>
	</table>
	<br />
	<table width="100%" style="border:1px solid #f1f1f1">
		<tr>
			<th colspan="2" style="background-color: #f1f1f1;">XẾP LỚP</th>
		</tr>
		<tr>
			<td colspan="2">
				<?php
					include_once '../classes/lophoc_bs.php';
					$DLopHoc = null;
					if($MaChuongTrinh != "")
					{
						$LopHoc = new lophoc_bs();
						$LopHoc->SetMaKhoaHoc($MaKhoa);
						$LopHoc->SetMaChuongTrinh($MaChuongTrinh);
						$LopHoc->SetMaThoiGianHoc($MaThoiGianHoc);
						$DLopHoc = $LopHoc->LayDanhSach();
					}
				?>
				<select name="LopHoc" id="" onclick="loadXMLTTLop(this.value)">
					<option value="0" >---- Chọn lớp học ----</option>
					<?php
						if($DLopHoc != null)
						{
							for($i = 0; $i < count($DLopHoc); $i++)
							{
								include_once '../classes/hocvien_bs.php';
								$HocVien = new hocvien_bs();
								$HocVien->SetMaLopHoc($DLopHoc[$i]['MaLopHoc']);
								$DHocVien = $HocVien->LayDanhSach();
								$SoHocVien = $DHocVien == null ? 0 : count($DHocVien);
								
								include_once '../classes/phonghoc_bs.php';
								$PhongHoc = new phonghoc_bs();
								$PhongHoc->SetMaPhong($DLopHoc[$i]['MaPhong']);
								$DPhongHoc = $PhongHoc->LayDanhSach();
								
								$SoLuongToiDa = $DPhongHoc == null ? 0 : $DPhongHoc[0]['SoMayHoatDong'];
								if($SoHocVien < $SoLuongToiDa)
								{
									echo '<option value="' . $DLopHoc[$i]['MaLopHoc'] . '">';
										echo $DLopHoc[$i]['MaLopHoc'] . ' [ ' . $SoHocVien . '/' . $SoLuongToiDa . ' ] ';
									echo '</option>';
								}
							}
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td width="50%">
				<a href="index.php?act=lophoc_them&<?php echo $MaKhoa != '' ? 'mkh=' . $MaKhoa . '&' : ''; ?>mct=<?php echo $MaChuongTrinh; ?>&mtg=<?php echo $MaThoiGianHoc; ?>" target="_blank" >Tạo lớp học ? </a>
			</td>
			<td width="50%">
				<input style="width:100%" type="submit" class="SubmitButton" onclick="return confirm('Bạn chắc chắn thêm học viên vào lớp đã chọn?')" value="Thêm"/>
			</td>
		</tr>
	</table>
	<br /> <br />
	<div id="result2"></div>
	
	</div>

</form>
<script type="text/javascript">
	function checkAll(bx) {
		var cbs = document.getElementsByTagName('input');
		for(var i=0; i < cbs.length; i++) {
			if(cbs[i].type == 'checkbox') {
				cbs[i].checked = bx.checked;
			}
		}
	};
	function checkCheckbox(checkboxId)
	{
		var chk = document.getElementById(checkboxId);
		if (chk)
		{
			if(chk.checked == true)
				chk.checked = false;
			else
				chk.checked = true;
		}
	};
</script>